package com.example.hewei.smarttransport.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.hewei.smarttransport.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 何伟 on 2017/4/5.
 */

public class AdminUsers extends BaseFragment {

    ListView mUserCarLV;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_admin_users, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setListView();
        setListener();
    }

    private void setListener() {
        mUserCarLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), "点击操作按钮，弹出对应窗口",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setListView() {
        LayoutInflater inflater=(LayoutInflater)getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.custom_user_header, null, false);
        mUserCarLV.addHeaderView(view, null ,false);
        mUserCarLV.setAdapter(new SimpleAdapter(getContext(), getData(),R.layout.custom_user_item, new String[]{"username","email","phone","registerTime"},new int[]{R.id.username, R.id.email,R.id.phone,R.id.registerTime}));
    }

    /**
     * 临时生成演示用的数据，数据应该来源于服务器
     * @return List<Map<String, Object>>对象
     */
    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for(int i=0;i<10;i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("username", "张"+(i+1));
            map.put("email", (6283932+i)+"@qq.com");
            map.put("phone", (13800000000L+i)+"");
            map.put("registerTime", "2017-01-01");
            list.add(map);
        }
        return list;
    }

    private void initView() {
        mUserCarLV= (ListView) getView().findViewById(R.id.users);
    }
}
